import useTabs from '@/utils/useTabs'
import {windows} from "codemirror/src/util/browser";

export default {
	methods: {
		async setGoBackOpt() {
			if(this.bpmStep.bpmLinkFlag=='2')
			{
				this.$refs.bpmLink.setBpmLink(this.bpmList.runId);
			}
			if (this.bpmStep.goBack == "1") {
				this.$confirm('您确定要回退当前流程吗?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(async () => {
					if(this.bpmFlow.ideaTextFlag=='1'&&(this.bpmStepRun.ideaText==''||this.bpmStepRun.ideaText==undefined))
					{
						this.$alert("会签意见不能为空！", "提示", {type: 'error'});
						return false;
					}
					this.$refs['preForm'].getFormData().then(async formData => {
						this.formData = formData
						var res = await this.$API.bpm.bpmFormData.saveBpmFormData.post(
							{
								formData: JSON.stringify(this.formData),
								flowId: this.bpmList.flowId,
								runId: this.bpmList.runId,
								flowTitle: this.bpmList.flowTitle,
								urgency: this.bpmList.urgency,
								stepRunId: this.bpmStepRun.stepRunId,
								ideaText: this.bpmStepRun.ideaText,
								passStatus: this.bpmStepRun.passStatus,
								stepAttach: this.bpmStepRun.stepAttach,
								runAttach: this.bpmList.runAttach
							}
						);
						if (res.code == 200) {
							var res1 = await this.$API.bpm.bpmStepRun.setGoBackOpt.post({runId: this.bpmList.runId, stepRunId: this.bpmStepRun.stepRunId});
							if (res1.code == 200) {
								this.$message.success("操作成功")
							} else {
								this.$alert(res1.message, "提示", {type: 'error'});
							}
							useTabs.close()
						} else {
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}).catch(error => {
						this.$message.error(error)
					})
				}).catch(() => {
				});

			} else if (this.bpmStep.goBack == "2") {
				if(this.bpmFlow.ideaTextFlag=='1'&&(this.bpmStepRun.ideaText==''||this.bpmStepRun.ideaText==undefined))
				{
					this.$alert("会签意见不能为空！", "提示", {type: 'error'});
					return false;
				}
				this.$confirm('您确定要回退当前流程吗?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(async () => {
					this.$refs['preForm'].getFormData().then(async formData => {
						this.formData = formData
						var res = await this.$API.bpm.bpmFormData.saveBpmFormData.post(
							{
								formData: JSON.stringify(this.formData),
								flowId: this.bpmList.flowId,
								runId: this.bpmList.runId,
								flowTitle: this.bpmList.flowTitle,
								urgency: this.bpmList.urgency,
								stepRunId: this.bpmStepRun.stepRunId,
								ideaText: this.bpmStepRun.ideaText,
								passStatus: this.bpmStepRun.passStatus,
								stepAttach: this.bpmStepRun.stepAttach,
								runAttach: this.bpmList.runAttach
							}
						);
						if (res.code == 200) {
							this.dialog.goBackSave = true
							this.$nextTick(() => {
								this.$refs.saveGoBackDialog.open().setData({runId: this.bpmList.runId, stepRunId: this.bpmStepRun.stepRunId})
							})
						} else {
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}).catch(error => {
						this.$message.error(error)
					})
				}).catch(() => {
				});
			}
		},
		handleGoBackSaveSuccess() {
			useTabs.close();
		},
		async getHandleBpmFormDataBean() {
			var res = await this.$API.bpm.bpmForm.getHandleBpmFormDataBean.get({flowId: this.bpmList.flowId, runId: this.bpmList.runId, stepRunId: this.bpmStepRun.stepRunId});
			if (res.code == 200) {
				this.$refs.preForm.setFormJson(res.data.formJson);
				this.bpmStep = res.data.bpmStep;
				this.bpmFlow = res.data.bpmFlow;
				this.bpmStepRun = res.data.bpmStepRun;
				this.bpmList = res.data.bpmList;
				this.bpmStepRunList = res.data.allBpmRunStep;
				this.waterMarkSubtext = res.data.waterMarkSubtext;
				this.waterMarkContent = res.data.waterMarkContent;
				this.followUser = res.data.followUser;
				this.childrenTable = res.data.childrenTable;
				useTabs.setTitle(this.bpmList.flowTitle)
			}else
			{
				this.$alert(res.message, "提示", {type: 'error'});
				useTabs.close();
			}
		},
		async getBpmCommIdeaList() {
			var res = await this.$API.bpm.bpmCommIdea.getBpmCommIdeaList.get();
			if (res.code == 200) {
				this.commIdeaTextList = res.data;
			}
		},
		async saveBpmFormDataChildTableData()
		{
			if(this.childrenTable!=undefined)
			{
				var childTableDates={};
				this.childrenTable.forEach(item=>{
					childTableDates[item] = this.$refs.preForm.getWidgetRef(item).field.options.tableData;
				})
				var res = await this.$API.bpm.bpmFormData.saveBpmFormDataChildTableData.post(
					{
						runId: this.bpmList.runId,
						stepId:this.bpmStep.stepId,
						childTableData:JSON.stringify(childTableDates)
					}
				);
				if(res.code!=200)
				{
					this.$alert(res.message, "提示", {type: 'error'});
				}
			}
		},
		saveFormData() {
			if (this.opType == "start") {
				this.$refs['preForm'].getFormData().then(async formData => {
					this.formData = formData
					var res = await this.$API.bpm.bpmFormData.createBpmFormData.post(
						{
							formData: JSON.stringify(this.formData),
							runId: this.bpmList.runId,
							flowId: this.bpmList.flowId,
							flowTitle: this.bpmList.flowTitle,
							urgency: this.bpmList.urgency,
							stepRunId: this.bpmStepRun.stepRunId,
							ideaText: this.bpmStepRun.ideaText,
							passStatus: this.bpmStepRun.passStatus,
							stepAttach: this.bpmStepRun.stepAttach,
							runAttach: this.bpmList.runAttach
						}
					);
					if (res.code == 200) {
						this.bpmList.id = res.data.bpmRunNo;
						this.opType = "edit";
						this.bpmStepRun.stepRunId = res.data.stepRunId;
						this.bpmList.runId = res.data.runId;
						if(this.bpmStep.bpmLinkFlag=='2')
						{
							this.$refs.bpmLink.setBpmLink(this.bpmList.runId);
						}
						this.saveBpmFormDataChildTableData();
						this.$message.success("操作成功");
						let newUrl = window.location.href.split("?")[0]+"?runId="+this.bpmList.runId+"&stepRunId="+this.bpmStepRun.stepRunId+"&flowId="+this.bpmList.flowId;
						history.replaceState('', '', newUrl)
					}
				}).catch(error => {
					this.$message.error(error)
				})
			} else {
				this.saveBpmFormDataChildTableData();
				this.$refs['preForm'].getFormData().then(async formData => {
					this.formData = formData
					var res = await this.$API.bpm.bpmFormData.saveBpmFormData.post(
						{
							formData: JSON.stringify(this.formData),
							flowId: this.bpmList.flowId,
							runId: this.bpmList.runId,
							flowTitle: this.bpmList.flowTitle,
							urgency: this.bpmList.urgency,
							stepRunId: this.bpmStepRun.stepRunId,
							ideaText: this.bpmStepRun.ideaText,
							passStatus: this.bpmStepRun.passStatus,
							stepAttach: this.bpmStepRun.stepAttach,
							runAttach: this.bpmList.runAttach
						}
					);
					if (res.code == 200) {
						this.$message.success("操作成功")
					}
					if(this.bpmStep.bpmLinkFlag=='2')
					{
						this.$refs.bpmLink.setBpmLink(this.bpmList.runId);
					}
				}).catch(error => {
					this.$message.error(error)
				})
			}
		},
		changeCommIdea(val) {
			this.bpmStepRun.ideaText = val
		},
		async getCreateBpmFormDataBean() {
			var res = await this.$API.bpm.bpmForm.getCreateBpmFormDataBean.get(this.bpmList);
			if (res.code == 200) {
				this.$refs.preForm.setFormJson(res.data.formJson);
				this.bpmStep = res.data.bpmStep;
				this.waterMarkSubtext = res.data.waterMarkSubtext;
				this.waterMarkContent = res.data.waterMarkContent;
				this.childrenTable = res.data.childrenTable;
				this.bpmFlow = res.data.bpmFlow;
			}
		},
		async setIdeaText(){
			if (this.bpmStep.approvalFlag == "2") {
				if (this.bpmStepRun.ideaText == '') {
					this.$alert('请填写会签意见', "提示", {type: 'error'});
					return;
				}
			}
			if(this.bpmFlow.flowType=='2')
			{
				let s = await this.$API.bpm.bpmLinkUser.nextBpmLinkBpm.post({
					runId: this.bpmList.runId,
					stepRunId: this.bpmStepRun.stepRunId,
					ideaText: this.bpmStepRun.ideaText,
					passStatus: this.bpmStepRun.passStatus,
					stepAttach: this.bpmStepRun.stepAttach,
				});
				if(s.code==200)
				{
					this.$message.success(s.message);
					useTabs.close();
				}else {
					this.$alert(s.message, "提示", {type: 'error'})
				}
			}else {
				var res = await this.$API.bpm.bpmStepRun.setIdeaText.post(
					{
						runId: this.bpmList.runId,
						stepRunId: this.bpmStepRun.stepRunId,
						ideaText: this.bpmStepRun.ideaText,
						passStatus: this.bpmStepRun.passStatus,
						stepAttach: this.bpmStepRun.stepAttach,
					}
				);
				if (res.code == 200) {
					this.$message.success(res.message);
					useTabs.close()
				} else {
					this.$alert(res.message, "提示", {type: 'error'})
					useTabs.close()
				}
			}
		},
		async sendBpmToNext() {
			this.saveBpmFormDataChildTableData();
			if(this.bpmFlow.flowType==3&&this.bpmStepRun.preStepId!=undefined)
			{
				this.$refs['preForm'].getFormData().then(async formData => {
					this.formData = formData
					var res = await this.$API.bpm.bpmFormData.saveBpmFormData.post(
						{
							formData: JSON.stringify(this.formData),
							runId: this.bpmList.runId,
							flowId: this.bpmList.flowId,
							flowTitle: this.bpmList.flowTitle,
							urgency: this.bpmList.urgency,
							stepRunId: this.bpmStepRun.stepRunId,
							ideaText: this.bpmStepRun.ideaText,
							passStatus: this.bpmStepRun.passStatus,
							stepAttach: this.bpmStepRun.stepAttach,
							runAttach: this.bpmList.runAttach
						}
					);
					if (res.code == 200) {
						let s = await this.$API.bpm.bpmStepPreset.nextBpmStepPreset.post({
							runId: this.bpmList.runId,
							stepRunId: this.bpmStepRun.stepRunId
						});
						if (s.code == 200) {
							this.$message.success(s.message);
							useTabs.close();
						} else {
							this.$alert(s.message, "提示", {type: 'error'})
						}
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				});
				if(this.bpmStep.bpmLinkFlag=='2')
				{
					this.$refs.bpmLink.setBpmLink(this.bpmList.runId);
				}
			}else{
				if (this.bpmStep.approvalFlag == "2") {
					if (this.bpmStepRun.ideaText == '') {
						this.$alert('请填写会签意见', "提示", {type: 'error'});
						return;
					}
				}
				if (this.opType == "start") {
					this.$refs['preForm'].getFormData().then(async formData => {
						this.formData = formData
						var res = await this.$API.bpm.bpmFormData.createBpmFormData.post(
							{
								formData: JSON.stringify(this.formData),
								runId: this.bpmList.runId,
								flowId: this.bpmList.flowId,
								flowTitle: this.bpmList.flowTitle,
								urgency: this.bpmList.urgency,
								stepRunId: this.bpmStepRun.stepRunId,
								ideaText: this.bpmStepRun.ideaText,
								passStatus: this.bpmStepRun.passStatus,
								stepAttach: this.bpmStepRun.stepAttach,
								runAttach: this.bpmList.runAttach
							}
						);
						if (res.code == 200) {
							this.bpmList.id = res.data.bpmRunNo;
							this.bpmStepRun.stepRunId = res.data.stepRunId;
							this.bpmList.runId = res.data.runId;
							if (this.bpmStep.passEndFlag == "1"&&this.bpmStepRun.passStatus=="0") {
								this.$confirm('由于审批不通过,是否结束流程？','提示', {
									type: 'warning',
									confirmButtonText: '确定',
								}).then(async() => {
									var o = await this.$API.bpm.bpmList.doNotPassEndBpm.post({runId:this.bpmList.runId,stepRunId:this.bpmStepRun.stepRunId});
									if(o.code==200)
									{
										useTabs.close();
									}else {
										this.$alert(o.message, "提示", {type: 'error'})
									}
								}).catch(() => {
									//取消退出
								})
							}else {
								if(this.bpmFlow.flowType=="2")
								{
									if(this.testOrderUserInfo())
									{
										var r = await this.$API.bpm.bpmLinkUser.setBpmLinkUserByBpm.post(
											{
												runId:this.bpmList.runId,
												stepRunId:this.bpmStepRun.stepRunId,
												orderUser:JSON.stringify(this.orderUser),
												urgency: this.bpmList.urgency,
												remindNextUser:this.form.remindNextUser,
												msgContent:this.form.msgContent,
												autoSendUser:this.form.autoSendUser
											}
										);
										if(r.code==200)
										{
											useTabs.close();
										}else {
											this.$alert(r.message, "提示", {type: 'error'})
										}
									}
								}else if(this.bpmFlow.flowType=="3"){
									this.dialog.presetSave = true
									this.$nextTick(() => {
										this.$refs.presetDialog.open();
									})
								}else {
									this.opType = "edit";
									this.sendDialog.save = true
									this.$nextTick(() => {
										this.$refs.sendSaveDialog.open()
									})
									let newUrl = window.location.href.split("?")[0]+"?runId="+this.bpmList.runId+"&stepRunId="+this.bpmStepRun.stepRunId+"&flowId="+this.bpmList.flowId;
									history.replaceState('', '', newUrl)
								}
							}
							if(this.bpmStep.bpmLinkFlag=='2')
							{
								this.$refs.bpmLink.setBpmLink(this.bpmList.runId);
							}
						} else {
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}).catch(error => {
						this.$message.error(error)
					})
				} else {
					this.$refs['preForm'].getFormData().then(async formData => {
						this.formData = formData
						var res = await this.$API.bpm.bpmFormData.saveBpmFormData.post(
							{
								formData: JSON.stringify(this.formData),
								flowId: this.bpmList.flowId,
								runId: this.bpmList.runId,
								flowTitle: this.bpmList.flowTitle,
								urgency: this.bpmList.urgency,
								stepRunId: this.bpmStepRun.stepRunId,
								ideaText: this.bpmStepRun.ideaText,
								passStatus: this.bpmStepRun.passStatus,
								stepAttach: this.bpmStepRun.stepAttach,
								runAttach: this.bpmList.runAttach
							}
						);
						if (res.code == 200) {
							if (this.bpmStep.passEndFlag == "1"&&this.bpmStepRun.passStatus=="0") {
								this.$confirm('由于审批不通过,是否结束流程？','提示', {
									type: 'warning',
									confirmButtonText: '确定',
								}).then(async() => {
									let o = await this.$API.bpm.bpmList.doNotPassEndBpm.post({runId:this.bpmList.runId,stepRunId:this.bpmStepRun.stepRunId});
									if(o.code==200)
									{
										useTabs.close();
									}else {
										this.$alert(o.message, "提示", {type: 'error'})
									}
								}).catch(() => {
									//取消退出
								})
							}else {
								if(this.bpmFlow.flowType=="2")
								{
									if(this.testOrderUserInfo())
									{
										let r = await this.$API.bpm.bpmLinkUser.setBpmLinkUserByBpm.post(
											{
												runId:this.bpmList.runId,
												stepRunId:this.bpmStepRun.stepRunId,
												orderUser:JSON.stringify(this.orderUser),
												urgency: this.bpmList.urgency,
												remindNextUser:this.form.remindNextUser,
												msgContent:this.form.msgContent,
												autoSendUser:this.form.autoSendUser
											}
										);
										if(r.code==200)
										{
											useTabs.close();
										}else {
											this.$alert(r.message, "提示", {type: 'error'})
										}
									}
								} else if(this.bpmFlow.flowType=="3"){
									this.dialog.presetSave = true
									this.$nextTick(() => {
										this.$refs.presetDialog.open();
									})
								} else {
									this.sendDialog.save = true
									this.$nextTick(() => {
										this.$refs.sendSaveDialog.open()
									})
								}
							}
						} else {
							this.$alert(res.message, "提示", {type: 'error'})
						}
						if(this.bpmStep.bpmLinkFlag=='2')
						{
							this.$refs.bpmLink.setBpmLink(this.bpmList.runId);
						}
					}).catch(error => {
						this.$message.error(error)
					})
				}
			}
		},
		async deleteBpmList() {
			this.$confirm('您确定中途撤销当前流程吗？','提示', {
				type: 'warning',
				confirmButtonText: '确定',
			}).then(async() => {
				var res = await this.$API.bpm.bpmList.deleteLogicBpm.post({runId:this.bpmList.runId});
				if (res.code == 200) {
					useTabs.close();
				} else {
					this.$alert(res.message, "提示", {type: 'error'})
				}
			}).catch(() => {
				//取消退出
			})
		},
		async changeBpmTitle() {
			if (!this.isShowTitle) {
				this.isShowTitle = true;
			} else {
				var res = await this.$API.bpm.bpmList.updateBpmList.post({runId: this.bpmList.runId, flowTitle: this.bpmList.flowTitle});
				if (res.code == 200) {
					this.$message.success(res.message);
					this.isShowTitle = false;
				} else {
					this.$alert(res.message, "提示", {type: 'error'})
				}
			}
		},
		async changeBpmFollow() {
			if (this.opType == "start") {
				this.$alert("执行保存操作后方可关注！", "提示", {type: 'warning'})
			}else{
				var res = await this.$API.bpm.bpmList.setBpmFollow.post({runId: this.bpmList.runId, isFollow: this.bpmList.followFlag});
				if (res.code == 200) {
					this.$message.success(res.message)
				} else {
					this.$alert(res.message, "提示", {type: 'error'})
				}
			}
		},
		setCommIdea() {
			this.commIdeaDialog.save = true
			this.$nextTick(() => {
				this.$refs.commIdeaDialog.open()
			})
		},
		closeTas() {
			useTabs.close()
		},
		addBpmStepRun(addType)
		{
			this.dialog.addStepSave = true
			this.$nextTick(() => {
				this.$refs.addStepDialog.open(addType)
			})
		},
		flowChartPreview() {
			this.$nextTick(() => {
				this.$router.replace({
					path: '/bpm/setting/bpmflow/flowchartpreview',
					query: {
						flowId: this.bpmList.flowId
					}
				})
			})
		},
		flowChartRun() {
			this.$nextTick(() => {
				this.$router.replace({
					path: '/bpm/setting/bpmflow/flowchartrun',
					query: {
						flowId: this.bpmList.flowId,
						runId:this.bpmList.runId
					}
				})
			})
		},
		addPublicFile(res, file)
		{
			let tArr = this.bpmList.runAttach.split(",");
			tArr.push(res.data.attachId);
			this.bpmList.runAttach = tArr.join(",");
		},
		addStepFile(res, file)
		{
			let tArr =[];
			if(this.bpmStepRun.stepAttach!==undefined)
			{
				tArr = this.bpmStepRun.stepAttach.split(",");
			}
			tArr.push(res.data.attachId);
			this.bpmStepRun.stepAttach = tArr.join(",");
		},
		setOrderBpmUser(row,index)
		{
			this.tempRow=row;
			this.tempOrderUser='';
			this.$nextTick(() => {
				this.$refs.selectBpmUserDialog.selectOrgUser();
			})
		},
		setOrderUser(userList)
		{
			this.tempRow.userAccountId=userList[0].accountId;
			this.tempRow.userName=userList[0].userName;
		},
		showFileVersion(attachId)
		{
			this.dialog.versionSave = true
			this.$nextTick(() => {
				this.$refs.fileVersionDialog.open(attachId)
			})
		},
		setSendToUser(row, index) {
			this.tempRow = row;
			this.tempSendToUser = '';
			this.$nextTick(() => {
				this.$refs.selectUserDialog.selectOrgUser();
			})
		},
		setSendToUserRes(userList) {
			let accountArr = [];
			let userNameArr = [];
			for (let i = 0; i < userList.length; i++) {
				accountArr.push(userList[i].accountId)
				userNameArr.push(userList[i].userName)
			}
			this.tempRow.sendToUser = accountArr.join(",");
			this.tempRow.sendToUserName = userNameArr.join(",");
			console.log(this.tempRow);
		},
		testOrderUserInfo() {
			if(this.orderUser.length==0)
			{
				this.$alert("顺序步骤节点不能为空！", "提示", {type: 'error'});
				return false;
			}else {
				for (let i = 0; i < this.orderUser.length; i++) {
					if (this.orderUser[i].userAccountId === '') {
						this.$alert("步骤节点存在无主办人情况，请删除后再提交！", "提示", {type: 'error'});
						return false;
					}
				}
				return true;
			}
		},
		importBpmData(){
			this.dialog.importSave = true
			this.$nextTick(() => {
				this.$refs.bpmImportDialog.open()
			})
		},
		setBpmData(data){
			this.$refs['preForm'].setFormData(data);
		},
		printBpm() {
			this.$nextTick(() => {
				window.open('/#/bpm/handle/print?runId=' + this.bpmList.runId+"&ddtab=true", "_blank");
			})
		},
	},
}
